package com.sat.client.adapter

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.sat.client.R
import com.sat.client.bean.DeviceInfo
import com.sat.client.bean.FileItem
import com.sat.client.utils.FileType

class FileItemAdapter : BaseQuickAdapter<FileItem, QuickViewHolder>() {
    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: FileItem?) {
        item?.run {
            val imvThumb = holder.getView<ImageView>(R.id.imv_thumb)
            val imvVideoFlag = holder.getView<ImageView>(R.id.imv_video_flag)
            imvVideoFlag.visibility = View.GONE
            holder.setText(R.id.tv_name, name)
            when (type) {
                FileType.DIRECTORY -> {
                    holder.setImageResource(R.id.imv_thumb, R.drawable.ic_directory)
                    holder.setText(R.id.tv_name, "$name(${contents?.size})")
                }

                FileType.IMAGE -> {
                    Glide.with(imvThumb).load(url)
                        .placeholder(R.drawable.ic_placeholder)
                        .into(imvThumb)
                }

                FileType.VIDEO -> {
                    // 使用Glide加载视频首帧
                    Glide.with(imvThumb)
                        .asBitmap() // 指定加载为Bitmap
                        .load(url) // 加载视频文件
                        .placeholder(R.drawable.ic_placeholder)
                        .into(imvThumb)
                    imvVideoFlag.visibility = View.VISIBLE
                }

                FileType.AUDIO -> {
                    holder.setImageResource(R.id.imv_thumb, R.drawable.ic_audio)
                }

                else -> {
                    holder.setImageResource(R.id.imv_thumb, R.drawable.ic_file)
                }
            }

        }

    }


    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        return QuickViewHolder(R.layout.layout_file_item, parent)
    }
}